package com.ciwong.epaper.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.Result_scoce;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.util.ScoreUtils;

import java.math.BigDecimal;

/**
 * Created by young on 2017/4/10.
 */
public class ScoreResultPage extends LinearLayout {

    public static final int DISPLAY_TYPE_LSW = 0;
    public static final int DISPLAY_TYPE_WORD = 1;
    public static final int DISPLAY_TYPE_SENTENCE = 2;
    public static final int DISPLAY_TYPE_ONLINE_ANSWER = 3;
    private static final String TAG = "ScoreResultPage";
    /**
     * 结果页各个分数对应文字
     */
    private final static String[] SCORE_RESULT_TEXT = {"You can do better!",
            "Good!", "Well done!", "Wonderful!", "Awesome!"};
    /**
     * 得分
     */
    public RiseNumberTextView tx_score_result;
    private TextView tx_score_unit;
    //    private TextView tx_submit_success_tips;
    private TextView tx_title_text;
    private ImageView img_arrow_shuaxin;
    private TextView tx_work_time;
    private TextView btn_check_detail;
    public Button btn_return_last_page;
    /**
     * 总分
     */
    private TextView tx_work_score;
    private TextView countText; //句子个数
    private TextView tx_score_result_encourage;
    private LinearLayout rankLinear;

    private RelativeLayout handle_score_page;
    /**
     * 总分
     */
    private float mTotalScore;
    /**
     * 得分
     */
    private float mTestScore;

    private int mDisplayType = DISPLAY_TYPE_LSW;

    private TextView time_count_text;

    private TextView tx_check_later_tips;
    public TextView tv_submit_description;

    private View btn_ranking_list;


    /**
     * 排行榜跳转参数
     */
    private String mClientId;

    private String mUserId;

    private String mContentId;

    private boolean obj=false;


    public ScoreResultPage(Context context) {
        super(context);
        LayoutInflater.from(this.getContext()).inflate(R.layout.score_result_page, this);
        initView();
    }

    public ScoreResultPage(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(this.getContext()).inflate(R.layout.score_result_page, this);
        initView();
    }

    public ScoreResultPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(this.getContext()).inflate(R.layout.score_result_page, this);
        initView();
    }

    private TextView tv_corrections;
//    private View view_subjective;

    private void initView() {
        tx_score_result = (RiseNumberTextView) findViewById(R.id.tx_score_result);

        tx_score_unit = (TextView) findViewById(R.id.tx_score_unit);
//        tx_submit_success_tips = (TextView) findViewById(R.id.tx_submit_success_tips);
        img_arrow_shuaxin = (ImageView) findViewById(R.id.img_arrow_shuaxin);
        tx_work_time = (TextView) findViewById(R.id.tx_work_time);
        btn_check_detail = (TextView) findViewById(R.id.btn_check_detail);
        btn_return_last_page = (Button) findViewById(R.id.btn_return_last_page);
        tx_title_text = (TextView) findViewById(R.id.tx_title_text);
        time_count_text = (TextView) findViewById(R.id.time_count_text);
//        view_subjective = findViewById(R.id.view_subjective);

        tv_corrections = (TextView) findViewById(R.id.tv_corrections);

        btn_return_last_page.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });

        tx_work_score = (TextView) findViewById(R.id.tx_work_score);
        countText = (TextView) findViewById(R.id.count_text);
        tx_score_result_encourage = (TextView) findViewById(R.id.tx_score_result_encourage);

        tx_check_later_tips = (TextView) findViewById(R.id.tx_check_later_tips);


        rankLinear = (LinearLayout) findViewById(R.id.rank_linear);

//
//        btn_ranking_list = findViewById(R.id.btn_ranking_list);

        tv_submit_description = (TextView) findViewById(R.id.tv_submit_description);

        handle_score_page = (RelativeLayout) findViewById(R.id.handle_score_page);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) handle_score_page.getLayoutParams();
        layoutParams.width=getResources().getDisplayMetrics().widthPixels;
        layoutParams.height=(int)(0.75* getResources().getDisplayMetrics().widthPixels);
        handle_score_page.setLayoutParams(layoutParams);


        findViewById(R.id.iv_close_page).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ((Activity) getContext()).finish();

            }
        });

    }

    public void setRankLinIsVis(int visibility) {
        if (rankLinear != null) {
            rankLinear.setVisibility(visibility);
            findViewById(R.id.rank_lin).setVisibility(visibility);
        }
    }


    public void setRankListPara(String clientId, String userId, String contentId) {

        mClientId = clientId;
        mUserId = userId;
        mContentId = contentId;
    }


    public void setSueButText(String text) {
        btn_return_last_page.setText(text);
    }

    public void setTimeTitleText(String title) {
        ((TextView) findViewById(R.id.time_count_text)).setText(title);
    }


    public void setCountText(String text) {
        countText.setText(text);
    }

    public void setScoreText(String text) {
        tx_work_score.setText(text);
    }

    /**
     * @param evaluateSuccess 评分是否成功
     * @param score           得分
     * @param lsMode
     */
    public void setEvaluateState(int type  ,boolean evaluateSuccess, String score, int lsMode) {
        //
        if(type==1){//线上做答
            tx_score_result.setVisibility(VISIBLE);
            tx_score_result.setText(getResources().getString(R.string.make_score_ing3));
            tx_score_unit.setVisibility(GONE);
            tx_score_result_encourage.setVisibility(GONE);
            tx_check_later_tips.setVisibility(GONE);
            img_arrow_shuaxin.setVisibility(VISIBLE);

        }
        else if (evaluateSuccess) {

            mTestScore = new Float(score);

            BigDecimal bd = new BigDecimal(score);

            //小数位数
            int decimalsCount = bd.scale();

            if (decimalsCount == 0) {
                int intScore = (int) mTestScore;
                tx_score_result.withNumber(intScore);
            } else {
                tx_score_result.withNumber(mTestScore);
            }
            // 得分用动画
            tx_score_result.setDuration(1000);
            tx_score_result.start();

            img_arrow_shuaxin.setVisibility(GONE);
            tx_score_unit.setVisibility(VISIBLE);
            if (lsMode == ListenSpeakUtil.LsMode.SUBJECTIVE_MODE) { //主观题模式
                tx_score_result.setVisibility(GONE);
                tv_corrections.setVisibility(VISIBLE);
                tx_score_unit.setVisibility(GONE);
                tx_score_result_encourage.setVisibility(GONE);

            } else {
               //view_subjective.setVisibility(GONE);
            }
            tx_check_later_tips.setVisibility(GONE);

            if (mTotalScore > 0) {
                if (lsMode == ListenSpeakUtil.LsMode.SUBJECTIVE_MODE) { //有主观题
                    tx_score_result_encourage.setVisibility(GONE);
                } else {
                    tx_score_result_encourage.setVisibility(VISIBLE);
                }
                BigDecimal totalDec = new BigDecimal(mTotalScore);
                BigDecimal testDec = new BigDecimal(mTestScore);

                float percetn = testDec.divide(totalDec, 2, BigDecimal.ROUND_HALF_UP).floatValue();
                percetn = percetn * 100;
                int perScore = Float.valueOf(percetn).intValue();

                if (perScore <= 60) {
                    tx_score_result_encourage.setText(SCORE_RESULT_TEXT[0]);
                } else if (perScore < 70 && perScore >= 60) {
                    tx_score_result_encourage.setText(SCORE_RESULT_TEXT[1]);
                } else if (perScore < 80 && perScore >= 70) {
                    tx_score_result_encourage.setText(SCORE_RESULT_TEXT[2]);
                } else if (perScore < 90 && perScore >= 80) {
                    tx_score_result_encourage.setText(SCORE_RESULT_TEXT[3]);
                } else if (perScore >= 90) {
                    tx_score_result_encourage.setText(SCORE_RESULT_TEXT[4]);
                }

            }

        } else {

            if (lsMode == ListenSpeakUtil.LsMode.OWN_PRACTISE) {
                tx_check_later_tips.setText(getResources().getString(R.string.str_check_later_practice));
                tx_check_later_tips.setVisibility(VISIBLE);

            } else {
                tx_check_later_tips.setVisibility(VISIBLE);
                tx_check_later_tips.setText(getResources().getString(R.string.str_check_later_homework));
            }

            if(obj){
                tx_score_result.setText(getResources().getString(R.string.make_score_ing3));
            }else {
                tx_score_result.setText(getResources().getString(R.string.make_score_ing2));
                img_arrow_shuaxin.setVisibility(VISIBLE);
            }

            tx_score_unit.setVisibility(GONE);
        }
    }


    public void  setEvaluateLastState(Result_scoce result_scoce){

        if(result_scoce.getStatus()==1||
                result_scoce.getStatus()==4){
            mTestScore = new Float(result_scoce.getActualScore());
            BigDecimal bd = new BigDecimal(result_scoce.getActualScore());

            //小数位数
            int decimalsCount = bd.scale();

            if (decimalsCount == 0) {
                int intScore = (int) mTestScore;
                tx_score_result.withNumber(intScore);
            } else {
                tx_score_result.withNumber(mTestScore);
            }
            // 得分用动画
            tx_score_result.setDuration(1000);
            tx_score_result.start();
            tx_score_result.setVisibility(VISIBLE);
            img_arrow_shuaxin.setVisibility(GONE);
            tx_score_unit.setVisibility(VISIBLE);
            tv_corrections.setVisibility(GONE);
            tx_check_later_tips.setVisibility(GONE);

            BigDecimal totalDec = new BigDecimal(mTotalScore);
            BigDecimal testDec = new BigDecimal(mTestScore);
            float percetn;
            try {
                percetn = testDec.divide(totalDec, 2, BigDecimal.ROUND_HALF_UP).floatValue();
            }catch (Exception e){
                percetn=0;
            }
            percetn = percetn * 100;
            int perScore = Float.valueOf(percetn).intValue();

            if (perScore <= 60) {
                tx_score_result_encourage.setText(SCORE_RESULT_TEXT[0]);
            } else if (perScore < 70 && perScore >= 60) {
                tx_score_result_encourage.setText(SCORE_RESULT_TEXT[1]);
            } else if (perScore < 80 && perScore >= 70) {
                tx_score_result_encourage.setText(SCORE_RESULT_TEXT[2]);
            } else if (perScore < 90 && perScore >= 80) {
                tx_score_result_encourage.setText(SCORE_RESULT_TEXT[3]);
            } else if (perScore >= 90) {
                tx_score_result_encourage.setText(SCORE_RESULT_TEXT[4]);
            }
        }else if(result_scoce.getStatus()==16){
            tx_score_result.setVisibility(GONE);
            tv_corrections.setVisibility(VISIBLE);
            tx_score_unit.setVisibility(GONE);
            tx_score_result_encourage.setVisibility(GONE);
            tx_check_later_tips.setVisibility(VISIBLE);
            tx_score_result.setText(getResources().getString(R.string.make_score_ing2));
            img_arrow_shuaxin.setVisibility(VISIBLE);

        }else {
            tx_score_result.setVisibility(GONE);
            tv_corrections.setVisibility(VISIBLE);
            tx_score_unit.setVisibility(GONE);
            tx_score_result_encourage.setVisibility(GONE);
            tx_check_later_tips.setVisibility(VISIBLE);
            tx_score_result.setText(getResources().getString(R.string.make_score_ing3));
            img_arrow_shuaxin.setVisibility(VISIBLE);
        }


    }

    public void goneshuaxin(boolean ishasobj){
        if(ishasobj){
            obj=ishasobj;
            img_arrow_shuaxin.setVisibility(GONE);
        }else {
            img_arrow_shuaxin.setVisibility(VISIBLE);
        }

    }

    public void setToalScore(float score) {

        mTotalScore = score;


        tx_work_score.setText(ScoreUtils.getScoreDisFormat(score) + "分");

    }

    public void setWorkTime(String workTime) {
        tx_work_time.setText(workTime);
    }

    public void setTitleText(String titleText) {
        tx_title_text.setText(titleText);
    }


    public void setOnRefreshScoreListener(View.OnClickListener listener) {
        img_arrow_shuaxin.setOnClickListener(listener);
    }

    public void setOnCheckDetailListener(View.OnClickListener listener) {
        btn_check_detail.setOnClickListener(listener);
    }

    public void setOnCheckRankingListListener(View.OnClickListener listListener) {
        btn_ranking_list.setOnClickListener(listListener);
    }


    public void setOnToRankListener(View.OnClickListener listener) {
        if (rankLinear != null) {
            rankLinear.setOnClickListener(listener);
        }
    }


    /**
     * @param type 单词跟读，句子跟读，听说模考
     */
    public void setDispayType(int type) {

        switch (type) {

            case DISPLAY_TYPE_LSW:
                time_count_text.setText(getResources().getString(R.string.str_use_time_answer));
                countText.setText("总    分");
                break;

            case DISPLAY_TYPE_WORD:
                time_count_text.setText(getResources().getString(R.string.str_use_time_follow));
                countText.setText("单词个数");
                break;

            case DISPLAY_TYPE_SENTENCE:
                time_count_text.setText(getResources().getString(R.string.str_use_time_follow));
                countText.setText("句子个数");
                break;
            case DISPLAY_TYPE_ONLINE_ANSWER:
//                time_count_text.setText(getResources().getString(R.string.str_use_time_follow));
//                countText.setText("句子个数");
                countText.setText("总    分");
                time_count_text.setText(getResources().getString(R.string.str_use_time_answer));
                break;

        }
    }

    public CharSequence wordDyeing(String beforText, String afterText) {
        ForegroundColorSpan colorSpanBefor = new ForegroundColorSpan(Color.parseColor("#F67177"));
        ForegroundColorSpan colorSpanAfter = new ForegroundColorSpan(Color.parseColor("#52CC8F"));
        SpannableStringBuilder spannableString = new SpannableStringBuilder(beforText);
        spannableString.setSpan(colorSpanBefor, beforText.indexOf("次") - 1, beforText.indexOf("次"), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.append(afterText);
        spannableString.setSpan(colorSpanAfter, spannableString.length() - 2, spannableString.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
